AtomicReferenceArray হল java.util.concurrent.atomic প্যাকেজের একটি ক্লাস, যা reference arrays এর উপরে atomic operations সম্পন্ন করতে ব্যবহৃত হয়। এটি Generics এর সাথে ব্যবহার করা যায় এবং থ্রেড-সেফ (thread-safe) পদ্ধতিতে reference types পরিচালনা করতে সাহায্য করে।
AtomicReferenceArray কী?
- এটি একটি atomic array of objects যেখানে প্রতিটি অপারেশন thread-safe।
- Generics ব্যবহার করে বিভিন্ন ধরনের ডেটা (যেমন String, Object, Custom Class) এর উপর কাজ করতে পারে।
- এটি লক-মুক্ত (lock-free) অপারেশন সরবরাহ করে, যার ফলে উচ্চ কার্যক্ষমতা বজায় থাকে।
- Thread Safety নিশ্চিত করে একাধিক থ্রেড একযোগে ডেটা ম্যানিপুলেট করলেও ডেটা সঠিক থাকে।
AtomicReferenceArray এর প্রধান বৈশিষ্ট্য
- Atomic Operations: প্রতিটি অপারেশন যেমন get(), set(), এবং compareAndSet() indivisible বা অখণ্ড।
- Thread-Safe: একাধিক থ্রেড একই সময়ে ডেটা অ্যাক্সেস বা পরিবর্তন করলেও ডেটা দুর্নীতি হয় না।
- Generics Support: এটি যেকোনো reference type (Generics) এর জন্য ব্যবহারযোগ্য।
- Non-blocking Performance: লক ছাড়াই ডেটা পরিচালিত হয়, যা কার্যক্ষমতা বাড়ায়।
- Immutability of Size: একটি AtomicReferenceArray এর সাইজ তৈরি হওয়ার পরে পরিবর্তন করা যায় না।
AtomicReferenceArray তৈরি করা
import java.util.concurrent.atomic.AtomicReferenceArray;
public class AtomicReferenceArrayExample {
public static void main(String[] args) {
// AtomicReferenceArray তৈরি
AtomicReferenceArray<String> atomicArray = new AtomicReferenceArray<>(5);
// ডেটা সেট করা
atomicArray.set(0, "Java");
atomicArray.set(1, "Python");
atomicArray.set(2, "C++");
// ডেটা পড়া
System.out.println("Index 0: " + atomicArray.get(0)); // Output: Java
System.out.println("Index 1: " + atomicArray.get(1)); // Output: Python
// compareAndSet ব্যবহার
boolean updated = atomicArray.compareAndSet(1, "Python", "JavaScript");
System.out.println("Updated: " + updated); // Output: true
System.out.println("Index 1: " + atomicArray.get(1)); // Output: JavaScript
}
}
AtomicReferenceArray এর মেথডসমূহ
১. get(int index)
নির্দিষ্ট index থেকে মান পড়ে।
String value = atomicArray.get(0); // Index 0 থেকে ডেটা পড়ে
২. set(int index, E newValue)
নির্দিষ্ট index এ একটি নতুন মান সেট করে।
atomicArray.set(1, "Python"); // Index 1 এ Python সেট
৩. compareAndSet(int index, E expected, E newValue)
যদি নির্দিষ্ট index এর বর্তমান মান expected এর সমান হয়, তবে এটি newValue দিয়ে আপডেট করে। এটি atomic check-and-set অপারেশন।
boolean isUpdated = atomicArray.compareAndSet(1, "Python", "JavaScript");
৪. getAndSet(int index, E newValue)
নির্দিষ্ট index এর বর্তমান মানটি রিটার্ন করে এবং একটি নতুন মান সেট করে।
String oldValue = atomicArray.getAndSet(2, "C#");
৫. length()
অ্যারের সাইজ রিটার্ন করে।
int size = atomicArray.length(); // আউটপুট: 5
AtomicReferenceArray ব্যবহার কেন গুরুত্বপূর্ণ?
১. Thread-Safe Arrays
একাধিক থ্রেড যখন একটি reference array এর উপরে কাজ করে, তখন AtomicReferenceArray ডেটা সঠিকতা নিশ্চিত করে।
২. Generics এর সমর্থন
Generics এর মাধ্যমে এটি বিভিন্ন ডেটা টাইপের সাথে ব্যবহার করা যায়, যেমন Strings, Custom Objects, ইত্যাদি।
৩. Race Condition প্রতিরোধ
প্রতিটি অপারেশন atomic হওয়ার কারণে race condition প্রতিরোধ করা যায়।
৪. Performance Optimization
লক-মুক্ত (lock-free) অপারেশন পারফরম্যান্স বাড়ায় এবং synchronization overhead হ্রাস করে।
উদাহরণ: Custom Class এর সাথে Generics ব্যবহার
import java.util.concurrent.atomic.AtomicReferenceArray;
class Product {
String name;
double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return name + " - $" + price;
}
}
public class CustomClassExample {
public static void main(String[] args) {
// Custom Class এর জন্য AtomicReferenceArray
AtomicReferenceArray<Product> productArray = new AtomicReferenceArray<>(3);
// প্রোডাক্ট যুক্ত করা
productArray.set(0, new Product("Laptop", 999.99));
productArray.set(1, new Product("Smartphone", 599.99));
// প্রোডাক্ট পড়া
System.out.println("Index 0: " + productArray.get(0)); // Output: Laptop - $999.99
System.out.println("Index 1: " + productArray.get(1)); // Output: Smartphone - $599.99
// compareAndSet ব্যবহার
boolean updated = productArray.compareAndSet(1,
new Product("Smartphone", 599.99),
new Product("Tablet", 399.99));
System.out.println("Updated: " + updated); // Output: false
}
}
AtomicReferenceArray এর সুবিধা
- Thread-Safe Reference Management: অ্যারেতে একাধিক থ্রেড কাজ করলেও ডেটা নিরাপদ থাকে।
- Generics Support: যেকোনো টাইপের ডেটার সাথে ব্যবহার করা যায়।
- Race Condition প্রতিরোধ: থ্রেডের মধ্যে ডেটা কনসিস্টেন্সি নিশ্চিত করে।
- High Performance: লক ছাড়াই ডেটা পরিচালিত হয়।
AtomicReferenceArray এর সীমাবদ্ধতা
- Immutable Size: অ্যারের সাইজ তৈরি হওয়ার পরে পরিবর্তন করা যায় না।
- Complexity: সহজ অ্যারের তুলনায় ব্যবহার কিছুটা জটিল।
- Generics Comparisons: compareAndSet() শুধুমাত্র reference equality চেক করে, deep equality নয়।
উপসংহার
AtomicReferenceArray মাল্টি-থ্রেডেড প্রোগ্রামিংয়ে reference types পরিচালনার জন্য একটি কার্যকর সমাধান। এটি thread safety, atomicity, এবং high performance নিশ্চিত করে। Generics এর সমর্থনের মাধ্যমে এটি আরও কার্যকর হয়ে ওঠে, বিশেষত যেখানে ডেটা কনসিস্টেন্সি এবং রেস কন্ডিশন প্রতিরোধ গুরুত্বপূর্ণ।
Read more